package com.atguigu.edusocket.controller;


import com.atguigu.commonutils.R;
import com.atguigu.edusocket.entity.AlarmNotification;
import com.atguigu.edusocket.entity.EventQuery;
import com.atguigu.edusocket.service.AlarmNotificationService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.minio.errors.*;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.List;

/**
 * <p>
 * 报警数据推送 前端控制器
 * </p>
 *
 * @author ryan
 * @since 2021-11-26
 */
@RestController
@RequestMapping("/edusocket/alarm-notification")
public class AlarmNotificationController {

    @Autowired
    private AlarmNotificationService alarmNotificationService;

    @PostMapping("/addEvent")
    public R addEvent(@RequestBody AlarmNotification alarmNotification) throws IOException, ServerException, InvalidBucketNameException, InsufficientDataException, ErrorResponseException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        alarmNotificationService.saveAndNotify(alarmNotification);
        return R.ok();
    }

    @DeleteMapping("/deleteEvent/{id}")
    public R deleteEvent(@PathVariable String id){
        alarmNotificationService.removeById(id);
        return R.ok();
    }

    @GetMapping("/getEventById/{id}")
    public R getEventById(@PathVariable String id){
        AlarmNotification alarm = alarmNotificationService.getById(id);
        return R.ok().data("alarm", alarm);
    }

    @PostMapping("/updateEvent")
    public R updateEvent(@RequestBody AlarmNotification alarmNotification){
        alarmNotificationService.updateById(alarmNotification);
        return R.ok();
    }

    //多条件查询课程带分页
    @ApiOperation(value = "多条件查询课程带分页")
    @PostMapping("/pageEvent/{page}/{limit}")
    public R pageCourseCondition(@ApiParam(name = "page", value = "当前页码", required = true)@PathVariable Long page,
                                 @ApiParam(name = "limit", value = "每页记录数", required = true)@PathVariable Long limit,
                                 @RequestBody(required = false) EventQuery eventQuery){//通过封装courseQuery对象来直接传递查询条件
        Page<AlarmNotification> coursePage = new Page<>(page, limit);
        alarmNotificationService.pageQuery(eventQuery, coursePage);
        List<AlarmNotification> records = coursePage.getRecords();
//        coursePage.
        long total = coursePage.getTotal();
        return R.ok().data("total",total).data("rows",records);
    }



}

